package com.itheima.service.Impl;

import com.github.pagehelper.PageHelper;
import com.itheima.domain.Student;
import com.itheima.domain.User;
import com.itheima.mapper.UserMapper;
import com.itheima.service.UserService;
import com.itheima.utils.MapperUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.ArrayList;
import java.util.List;

/**
 * @param
 * @return
 */
public class UserServiceImpl implements UserService {
    @Override
    public List<Student> findAll() {
        //定义返回值
        List<Student> list = null;
        //工具类操作走起
        SqlSession session = MapperUtil.getSession();
        /*获取接口代理对象*/
        UserMapper mapper = session.getMapper(UserMapper.class);
        //调用接口中的方法
        list = mapper.findAll();
        MapperUtil.close(session);
        return list;
    }

    @Override
    public boolean save(Student user) {
        //定义返回值
        boolean flag = false;
        //工具类操作走起
        SqlSession session = MapperUtil.getSession();
        /*获取接口代理对象*/
        UserMapper mapper = session.getMapper(UserMapper.class);
        //调用接口中的方法
        int i = mapper.save(user);
        if (i > 0) {
            flag = true;
        }
        MapperUtil.close(session);
        return flag;
    }

    @Override
    public Student findByNum(String number) {
        //定义返回值
        Student student = null;
        //工具类操作走起
        SqlSession session = MapperUtil.getSession();
        /*获取接口代理对象*/
        UserMapper mapper = session.getMapper(UserMapper.class);
        //调用接口中的方法
        student = mapper.findByNum(number);
        MapperUtil.close(session);
        return student;
    }

    @Override
    public boolean update(Student user) {
        //定义返回值
        boolean flag = false;
        //工具类操作走起
        SqlSession session = MapperUtil.getSession();
        /*获取接口代理对象*/
        UserMapper mapper = session.getMapper(UserMapper.class);
        //调用接口中的方法
        int i = mapper.update(user);
        if (i > 0) {
            flag = true;
        }
        MapperUtil.close(session);
        return flag;
    }

    @Override
    public boolean deleteByNum(String number) {
        //定义返回值
        boolean flag = false;
        SqlSession session = MapperUtil.getSession();
        UserMapper mapper = session.getMapper(UserMapper.class);
        int i = mapper.deleteByNum(number);
        if (i > 0) {
            flag = true;
        }
        MapperUtil.close(session);
        return flag;
    }

    @Override
    public User login(String username, String password) {
        //工具类走起
        SqlSession session = MapperUtil.getSession();
        //获取接口代理对象
        UserMapper mapper = session.getMapper(UserMapper.class);
        //创建用户对象
        User user = new User();
        user.setUsername(username);
        user.setPwd(password);
        User u = mapper.login(user);
        //关流走人
        MapperUtil.close(session);
        return u;
    }

    @Override
    public List<Student> selectByPage(int pageNum, int pageSize) {
        List<Student> list = null;
        //工具类走起
        SqlSession session = MapperUtil.getSession();
        //获取接口代理对象
        UserMapper mapper = session.getMapper(UserMapper.class);
        //设置分页参数
        PageHelper.startPage(pageNum, pageSize);
        list = mapper.findAll();
        MapperUtil.close(session);
        return list;
    }
}
